<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>建立一个具有唯一文件名的文件</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.symlink.html">« symlink</a></li>
      <li style="float: right;"><a href="function.tmpfile.html">tmpfile »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>建立一个具有唯一文件名的文件</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.tempnam" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">tempnam</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">tempnam</span> &mdash; <span class="dc-title">建立一个具有唯一文件名的文件</span></p>

 </div>
 
  <div class="refsect1 description" id="refsect1-function.tempnam-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>tempnam</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$directory</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$prefix</code></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   在指定目录中建立一个具有唯一文件名的文件。如果该目录不存在或不可写，<span class="function"><strong>tempnam()</strong></span>
   会在系统临时目录中生成一个文件，并返回该文件包含文件名的完整路径。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.tempnam-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">directory</code></dt>

     <dd>

      <p class="para">
       将在其中创建临时文件名的目录。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">prefix</code></dt>

     <dd>

      <p class="para">
       产生临时文件的前缀。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        仅使用前缀的前 63 个字符，忽略其它字符。Windows 仅使用前缀的前三个字符。
       </span>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>

 
  <div class="refsect1 returnvalues" id="refsect1-function.tempnam-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回新的带路径的临时文件名，出错返回 <strong><code>false</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 changelog" id="refsect1-function.tempnam-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>7.1.0</td>
      <td>
       当回退到系统的临时目录时 <span class="function"><strong>tempnam()</strong></span> 会发出一个通知。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>

 
  <div class="refsect1 examples" id="refsect1-function.tempnam-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2229">
    <p><strong>示例 #1 <span class="function"><strong>tempnam()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$tmpfname&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">tempnam</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"FOO"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$tmpfname</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"writing&nbsp;to&nbsp;tempfile"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;do&nbsp;something&nbsp;here<br /><br /></span><span style="color: #0000BB">unlink</span><span style="color: #007700">(</span><span style="color: #0000BB">$tmpfname</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 
  <div class="refsect1 notes" id="refsect1-function.tempnam-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    如果 PHP 不能在指定的 <code class="parameter">directory</code>参数中创建文件，则退回到系统默认值。在
    NTFS 文件系统中，同样的情况也发生在 <code class="parameter">directory</code> 中文件数超过 65534 个的时候。
   </span>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.tempnam-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.tmpfile.html" class="function" rel="rdfs-seeAlso">tmpfile()</a> - 建立一个临时文件</span></li>
    <li class="member"><span class="function"><a href="function.sys-get-temp-dir.html" class="function" rel="rdfs-seeAlso">sys_get_temp_dir()</a> - 返回用于临时文件的目录</span></li>
    <li class="member"><span class="function"><a href="function.unlink.html" class="function" rel="rdfs-seeAlso">unlink()</a> - 删除文件</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>